#include "graphics.h"

using namespace::std;

graphics::graphics()
{

}

bool graphics::init(int width, int height)
{
  // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        cerr << "Unable to init SDL: " << SDL_GetError() << endl;
        return 1;
    }

    // when the program will end, quit SDL
    atexit(SDL_Quit);

    // create a new window
    m_screen = SDL_SetVideoMode(width,height, 32,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);

    //check if it worked
    if ( !m_screen )
    {
        cerr << "Unable to set 1280x1024 video: " << SDL_GetError() << endl;
        return 1;
    }
    return true;

}

void graphics::refresh()
{
    SDL_Flip(m_screen);
    SDL_FillRect(m_screen, 0, SDL_MapRGB(m_screen->format, 0, 0, 0)); // clear screen
}

void graphics::drawImage(SDL_Surface* surface, SDL_Rect* rect)
{
    SDL_BlitSurface(surface, NULL, m_screen, rect);
}

//permet de récuper une portion d'une surface
SDL_Surface* graphics::subSurface(SDL_Surface* surface, SDL_Rect* rect)
{
    SDL_Surface* tmpSurf;
    SDL_BlitSurface(surface, rect, tmpSurf, NULL);
    return tmpSurf;
}

graphics::~graphics()
{
    //dtor
}
